<?php
$pathArr = explode("/", $_SERVER["PHP_SELF"]);
$contextPath = $pathArr[1];
$rootPath = $_SERVER["DOCUMENT_ROOT"];
include_once $rootPath . "/" . $contextPath . "/apps/ManagerIUDS.php";
$managerIUDS = new ManagerIUDS();

$mode = $_GET["mode"];
if($mode == "update"){
	$getBooklingData = $managerIUDS->queryData("*", "booking", " WHERE booking_id = ".$_GET["booking_id"], "1", "=");
	$petDataList = $managerIUDS->queryData("pet_id,pet_name","pet","","2","");
	$getListRecreationRoom = $managerIUDS->queryData("recreation_room_number,price,recreation_room_limit_weight", "recreation_room", " WHERE recreation_room_id = ".$getBooklingData["recreation_room_id"], "1", "=");
}else{
	$getListRecreationRoom = $managerIUDS->queryData("recreation_room_number,price,recreation_room_limit_weight", "recreation_room", " WHERE recreation_room_id = ".$_GET["recreation_room_id"], "1", "=");
	if($getListRecreationRoom["recreation_room_limit_weight"]=="M"){
		$weigth = "25";
		$operator = ">";
	}else{	
		$weigth = "25";
		$operator = "<=";
	}
	$petDataList = $managerIUDS->queryData("pet_id,pet_name","pet"," WHERE pet_weight ".$operator ." '".$weigth."'","2","");
	$foodDataList = $managerIUDS->queryData("price,food_name,pet_weight","food"," WHERE pet_weight ='".$getListRecreationRoom["recreation_room_limit_weight"]."'","2","");

}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>.: Pet Detail :.</title>
<?php
require_once($rootPath . "/" . $contextPath . "/includes/css_inc.php");
require_once($rootPath . "/" . $contextPath . "/includes/js_inc.php");
?>
<script type="text/javascript">
			$(document).ready(function(){ 
				$("#save_data").click(function(){
					var result = true;
					if($("#pet_id").val()=="" || $("#pet_id").val()==null){
						alert("กรูราเลือกสัตว์เลี้ยงที่จะเข้าพัก");
						$("#pet_id").focus(); 
						result = false
					}else if($("#start_date").val()=="" || $("#start_date").val()==null){
						alert("เลือกวันเริ่มต้นของการเข้าพัก");
						$("#start_date").focus(); 
						result = false
					}else if($("#end_date").val()=="" || $("#end_date").val()==null){
						alert("เลือกวันสุดท้ายของการเข้าพัก");
						$("#end_date").focus(); 
						result = false
					}
					if(result){
						$("#frm1").submit();
					}
				});
				$(function() {
					$( "#combobox" ).combobox();
				});
				$( "#start_date" ).datepicker({
					showOn: "button",
					buttonImage: "../images/calendar.gif",
					changeMonth: true,
					changeYear: true,
					buttonImageOnly: true,
					dateFormat: 'dd/mm/yy'
				});
				$( "#end_date" ).datepicker({
					showOn: "button",
					buttonImage: "../images/calendar.gif",
					changeMonth: true,
					changeYear: true,
					buttonImageOnly: true,
					dateFormat: 'dd/mm/yy'
				});
				$( "#start_date" ).change(function(){
						$.ajax({
    		    					type: "GET",
									 url: "../actions/booking_action.php",
    		    					data:{mode:"checkDate",startDate:$("#start_date").val()},
									contentType: "application/json; charset=utf-8", 
									dataType: "json",
    		    					async: false,
    		    		            cache: false, 
    		    					success: function(data){
										startChange(data.result);
    		    					},
    		    					error: function(xhr,state,exception) { alert("exception = > " + exception); }
    		    				});//end ajax
				});
				$('#end_date').change(function(){
					 var splitStartDate = $("#start_date").val().split("/");
					 var splitEndDate = $("#end_date").val().split("/");
					var startDate = "";
					startDate = splitStartDate[0].substring(0,splitStartDate[0].length-1);
					if(startDate == "0"){
						startDate =  splitStartDate[0].substring(1,splitStartDate[0].length);
					}else{
						startDate = splitStartDate[0];
					}
					 if(parseInt(splitStartDate[0]) > parseInt(splitEndDate[0])){
						 alert("กรุณากรอกวันสุดท้ายใหม่เนื่องจากกรอกข้อมูลไม่ถูกต้อง");
						 $("#end_date").val("");
						 $("#end_date").focus();
					 }else if(parseInt(splitStartDate[0]) == parseInt(splitEndDate[0])){
						 alert("ไม่สามารถกรอกวันเดียวกันได้");
						 $("#end_date").val("");
						 $("#end_date").focus();
					 }
					 else{
						 var sum = (parseInt(splitEndDate[0]) - parseInt(startDate)); 
						 if(sum == 0){
							 sum =  (parseInt(splitEndDate[0]) - parseInt(startDate))+1; 
						 }
						 sum = sum * $("#DataPrice").val(); 
						 $("#price").val(sum);
					 }
				});
					$("#food_price").change(function(){
						$("#price").val("");
						var splitStartDate = $("#start_date").val().split("/");
						var splitEndDate = $("#end_date").val().split("/");
						//alert(splitEtartDate);
						if((splitStartDate == "" || splitStartDate == null)&&(splitEndDate == "" || splitEndDate == null)){
							//sum = sum + parseInt($("#price").val())+parseInt($("#food_price").val());
							$("#price").val($("#food_price").val());
						}else{
							var sum = (parseInt(splitEndDate[0]) - parseInt(splitStartDate[0]))+1; 
							sum = sum * $("#DataPrice").val(); 
							$("#price").val(sum);
							sum = sum + parseInt($("#price").val())+parseInt($("#food_price").val());
							$("#price").val(sum);
						}
					});
					function startChange(result){
					if(result=="Y"){
						if($( "#mode" ).val()=="insertRoomNotFree" && $( "#start_date" ).val() != $( "#hidden_start_date" ).val() && $( "#hidden_start_date" ).val() !="" && $( "#hidden_start_date" ).val() != null){
							$( "#start_date" ).val($( "#hidden_start_date" ).val()) ;
						}else{					
							var splitStartDate = $("#start_date").val().split("/");
							var splitEndDate = $("#end_date").val().split("/");					
							var startDate = "";
							startDate = splitStartDate[0].substring(0,splitStartDate[0].length-1);
							if(startDate == "0"){
								startDate =  splitStartDate[0].substring(1,splitStartDate[0].length);
							}else{
								startDate = splitStartDate[0];
							}
								 var sum = (parseInt(splitEndDate[0]) - parseInt(startDate)); 
							if(sum == 0){
								 sum =  (parseInt(splitEndDate[0]) - parseInt(startDate))+1; 
							 }
							 sum = sum * $("#DataPrice").val(); 
							 $("#price").val(sum);
						}
					}else{
							alert("วันนี้มีผู้ทำการจองแล้วกรุณาเลือกวันใหม่");
							$( "#start_date" ).val("");
							$( "#end_date" ).val("");
							$( "#start_date" ).focus();
					}
					}
			}); 
			function removeOptionLast(){
			  	var selectBox =  $('#is_food').get(0);
			  	if(document.getElementById("is_food") == "N"){
	  				 for(var i=selectBox.options.length-1;i>=1;i--){
	  						selectBox.remove(i);
					 } 
			  	}
			}
			function showDetail(){
				var foodDetail = document.getElementById("food_detail"); 
				foodDetail.style.display ="block";
			}
			function hideDetail(){
				var foodDetail = document.getElementById("food_detail");
				var foodPeice = document.getElementById("food_price").value="0";  
				foodDetail.style.display ="none";
			}
		</script>
</head>
<body onload="hideDetail()">
<center>
<input type="hidden" name="DataPrice" id="DataPrice" value="<?=$getListRecreationRoom["price"]?>">
<form name="frm1" id="frm1" action="../actions/booking_action.php" method="post">
<table border="0" width="75%">
	<thead class="ui-widget">
		<tr class="ui-widget-header">
			<th colspan="2" align="left">&nbsp;&nbsp;&nbsp;รายละเอียดการจอง</th>
		</tr>
	</thead>
	<tbody class="ui-widget-content">
		<tr>
			<td align="right">หมายเลขห้องพัก :</td>
			<td><input type="text" name="recreation_room_number" id="recreation_room_number" readonly="readonly" value="<?=$getListRecreationRoom["recreation_room_number"];
			?>"
			></td>
		</tr>
		<tr>
			<td align="right">สัตว์เลี้ยง :</td>
			<td>
				<select name="pet_id" id="pet_id">
					<option value="">--- เลือก ---</option>
					<?php 
						foreach($petDataList as $value){
					?>
						<option value="<?=$value["pet_id"]?>"
						<?php
							if($value["pet_id"] == $getBooklingData["pet_id"]){
								echo 'selected="selected"'; 
							}
						?>
						><?=$value["pet_name"]?></option>
					<?php 
						}
					?>
				</select>
			</td>
		</tr>
		<tr>
			<td align="right">วันเริ่มต้นการเข้าพัก :</td>
			<td><input type="text" name="start_date" id="start_date"  value="<?=$ $getBooklingData["start_date"]?>"></td>
		</tr>
		<tr>
			<td align="right">วันสุดท้ายของการเข้าพัก :</td>
			<td><input type="text" name="end_date" id="end_date" value="<?= $getBooklingData["end_date"]?>"></td>
		</tr>
		<tr>
		<tr>
			<td align="right">อาหาร :</td>
			<td>
				<input type="radio" name="is_food" id="is_food" value="Y" checked="checked" onclick="hideDetail()" >นำมาเอง
				<input type="radio" name="is_food" id="is_food" value="N" onclick="showDetail()">อาหารทางคลีนิค
			</td>
		</tr>
		<tr>
			<td align="right" colspan="2">
				<table id="food_detail">
					<tr>
						<td align="right">ประเภทอาหาร :</td>
						<td>
							<?php 
								if($getListRecreationRoom["recreation_room_limit_weight"]=="M"){
									echo "อาหารสำหรับสัตว์เลี้ยงน้ำหนักน้อยกว่า 25";
								}else{
									echo "อาหารสำหรับสัตว์เลี้ยงน้ำหนักมากกว่า 25";
								}
							?>
						</td>
					</tr>
					<tr>
						<td align="right">อาหาร :</td>
						<td>
							<select name="food_price" id="food_price">
								<option value="0">--- เลือก ---</option>
								<?php 
									foreach ($foodDataList as $value){	
								?>
									<option value="<?=$value["price"]?>"><?=$value["food_name"]?></option>
								<?php 
									}
								?>
							</select>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td align="right">ราคา :</td>
			<td><input type="text" name="price" id="price" readonly="readonly" value="<?=$getBooklingData["price"]?>"></td>
		</tr>
		<tr>
			<td colspan="2" align="center">
			<input type="button" name="save_data" id="save_data" value="บันทึก">
			<?php 
			if($mode=="insert"){
			?>
				<input type="button" value="ยกเลิก"	onclick="window.location='recreation_room_list.php';"></td>
			<?php 
			}else{
			?>
				<input type="button" value="ยกเลิก"	onclick="window.location='booking_main.php';"></td>
			<?php 
			}
			?>
		</tr>
	</tbody>
</table>
<input type="hidden" id="mode" name="mode" value="<?=$mode?>">
<input	type="hidden" id="recreation_room_id" name="recreation_room_id" value=<?=$_GET["recreation_room_id"]?>>
</form>
</center>
</body>
</html>